/*
 * @Author: bor_fish 1219854025@qq.com
 * @Date: 2025-06-27 11:29:10
 * @LastEditors: bor_fish 1219854025@qq.com
 * @LastEditTime: 2025-06-29 00:57:20
 * @FilePath: /代码/LATENT_CONTROL_BOX_CAN.h.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "LATENT_CONTROL_BOX_CAN.h"
#include <stdexcept>
#include <cstring>

// 初始化静态阈值
LATENT_CONTROL_BOX_CAN::TimingThresholds LATENT_CONTROL_BOX_CAN::currentTimingThresholds = {
    200000, 80000, 120000, 400, 500, 5000
};
LATENT_CONTROL_BOX_CAN::TankLevelHeightThresholds LATENT_CONTROL_BOX_CAN::tanklevelHeightThresholds = {
    100
};
LATENT_CONTROL_BOX_CAN::PressureGageThresholds LATENT_CONTROL_BOX_CAN::pressuregageThresholds = {
    60
};


// 阈值设置实现
void LATENT_CONTROL_BOX_CAN::setTimingThresholds(const TimingThresholds& thresholds) {
    currentTimingThresholds = thresholds;
}
void LATENT_CONTROL_BOX_CAN::setTankLevelHeightThresholds(const TankLevelHeightThresholds& thresholds) {
    tanklevelHeightThresholds = thresholds;
}
void LATENT_CONTROL_BOX_CAN::setPressureGageThresholds(const PressureGageThresholds& thresholds) {
    pressuregageThresholds = thresholds;
}

// 阈值获取实现
LATENT_CONTROL_BOX_CAN::TimingThresholds LATENT_CONTROL_BOX_CAN::getCurrentThresholds() {
    return currentTimingThresholds;
}
LATENT_CONTROL_BOX_CAN::TankLevelHeightThresholds LATENT_CONTROL_BOX_CAN::getTankLevelHeightThresholds() {
    return tanklevelHeightThresholds;
}
LATENT_CONTROL_BOX_CAN::PressureGageThresholds LATENT_CONTROL_BOX_CAN::getPressureGageThresholds() {
    return pressuregageThresholds;
}


// 状态检查实现
int LATENT_CONTROL_BOX_CAN::checkOutputTiming(int64_t timeInterval) {
    auto thresholds = getCurrentThresholds();
    if (timeInterval > thresholds.output_no_response) return 3;
    if (timeInterval < thresholds.output_too_fast) return 2;
    if (timeInterval > thresholds.output_too_slow) return 1;
    return 0;
}

int LATENT_CONTROL_BOX_CAN::checkFeedbackTiming(int64_t timeBack) {
    auto thresholds = getCurrentThresholds();
    if (timeBack > thresholds.feedback_no_response) return 2;
    if (timeBack > thresholds.feedback_slow) return 1;
    return 0;
}


// 数据解析实现
// 0x341101
uint8_t LATENT_CONTROL_BOX_CAN::parseWorkPattern(const uint8_t* data, size_t offset) {
    return data[offset];
}

uint8_t LATENT_CONTROL_BOX_CAN::parseSeaValve(const uint8_t* data, size_t offset) {
    return data[offset + 1];
}

uint8_t LATENT_CONTROL_BOX_CAN::parseVentValveB(const uint8_t* data, size_t offset) {
    return data[offset + 2];
}

uint8_t LATENT_CONTROL_BOX_CAN::parseWaterTankVentValveA1(const uint8_t* data, size_t offset) {
    return data[offset + 3];
}

uint8_t LATENT_CONTROL_BOX_CAN::parseWaterTankVentValveA2(const uint8_t* data, size_t offset) {
    return data[offset + 4];
}

uint8_t LATENT_CONTROL_BOX_CAN::parseWaterTankVentValveA3(const uint8_t* data, size_t offset) {
    return data[offset + 5];
}

uint8_t LATENT_CONTROL_BOX_CAN::parseWaterTankVentValveA4(const uint8_t* data, size_t offset) {
    return data[offset + 6];
}
// 0x341102
uint8_t LATENT_CONTROL_BOX_CAN::parseAirSourceSelection(const uint8_t* data, size_t offset) {
    return data[offset];
}
uint8_t LATENT_CONTROL_BOX_CAN::parseWaterTankBlow14(const uint8_t* data, size_t offset) {
    return data[offset + 1];
}
uint8_t LATENT_CONTROL_BOX_CAN::parseWaterTankBlow23(const uint8_t* data, size_t offset) {
    return data[offset + 2];
}
// 0x341203
uint8_t LATENT_CONTROL_BOX_CAN::parseOperationModeStatus(const uint8_t* data, size_t offset) {
    return data[offset];
}
uint8_t LATENT_CONTROL_BOX_CAN::parseWorkingModeStatus(const uint8_t* data, size_t offset) {
    return data[offset + 1];
}
// 0x341204
uint16_t LATENT_CONTROL_BOX_CAN::parsePort1TankLevel(const uint8_t data[], size_t offset) {
    return (data[offset + 1] << 8) | data[offset + 0];
}
uint16_t LATENT_CONTROL_BOX_CAN::parseStarboard1TankLevel(const uint8_t* data, size_t offset) {
    return (data[offset + 3] << 8) | data[offset + 2];
}
uint16_t LATENT_CONTROL_BOX_CAN::parsePort2TankLevel(const uint8_t data[], size_t offset) {
    return (data[offset + 5] << 8) | data[offset + 4];
}
uint16_t LATENT_CONTROL_BOX_CAN::parseStarboard2TankLevel(const uint8_t* data, size_t offset) {
    return (data[offset + 7] << 8) | data[offset + 6];
}
// 0x341205
uint16_t LATENT_CONTROL_BOX_CAN::parsePort3TankLevel(const uint8_t data[], size_t offset) {
    return (data[offset + 1] << 8) | data[offset + 0];
}
uint16_t LATENT_CONTROL_BOX_CAN::parseStarboard3TankLevel(const uint8_t* data, size_t offset) {
    return (data[offset + 3] << 8) | data[offset + 2];
}
uint16_t LATENT_CONTROL_BOX_CAN::parsePort4TankLevel(const uint8_t data[], size_t offset) {
    return (data[offset + 5] << 8) | data[offset + 4];
}
uint16_t LATENT_CONTROL_BOX_CAN::parseStarboard4TankLevel(const uint8_t* data, size_t offset) {
    return (data[offset + 7] << 8) | data[offset + 6];
}
// 0x341206
uint16_t LATENT_CONTROL_BOX_CAN::parseVentvalveAStatus(const uint8_t data[], size_t offset) {
    return (data[offset + 1] << 8) | data[offset + 0];
}
uint16_t LATENT_CONTROL_BOX_CAN::parseVentvalveBStatus(const uint8_t* data, size_t offset) {
    return (data[offset + 3] << 8) | data[offset + 2];
}
uint16_t LATENT_CONTROL_BOX_CAN::parseSeaValveStatus(const uint8_t data[], size_t offset) {
    return (data[offset + 5] << 8) | data[offset + 4];
}
uint8_t LATENT_CONTROL_BOX_CAN::parseHighPressureBlowoutValveStatus(const uint8_t* data, size_t offset) {
    return data[offset + 6];
}
// 0x341207
uint16_t LATENT_CONTROL_BOX_CAN::parsePressureGage1(const uint8_t data[], size_t offset) {
    return (data[offset + 1] << 8) | data[offset + 0];
}
uint16_t LATENT_CONTROL_BOX_CAN::parsePressureGage2(const uint8_t* data, size_t offset) {
    return (data[offset + 3] << 8) | data[offset + 2];
}
uint16_t LATENT_CONTROL_BOX_CAN::parsePressureGage3(const uint8_t data[], size_t offset) {
    return (data[offset + 5] << 8) | data[offset + 4];
}
uint16_t LATENT_CONTROL_BOX_CAN::parsePressureGage4(const uint8_t* data, size_t offset) {
    return (data[offset + 7] << 8) | data[offset + 6];
}
// 0x341208
uint16_t LATENT_CONTROL_BOX_CAN::parsePressureGage5(const uint8_t data[], size_t offset) {
    return (data[offset + 1] << 8) | data[offset + 0];
}
uint16_t LATENT_CONTROL_BOX_CAN::parsePressureGage6(const uint8_t* data, size_t offset) {
    return (data[offset + 3] << 8) | data[offset + 2];
}
uint16_t LATENT_CONTROL_BOX_CAN::parsePressureGage7(const uint8_t data[], size_t offset) {
    return (data[offset + 5] << 8) | data[offset + 4];
}
uint16_t LATENT_CONTROL_BOX_CAN::parsePressureGage8(const uint8_t* data, size_t offset) {
    return (data[offset + 7] << 8) | data[offset + 6];
}
// 0x341209
uint16_t LATENT_CONTROL_BOX_CAN::parsePressureGage9(const uint8_t data[], size_t offset) {
    return (data[offset + 1] << 8) | data[offset + 0];
}
uint16_t LATENT_CONTROL_BOX_CAN::parsePressureGage10(const uint8_t* data, size_t offset) {
    return (data[offset + 3] << 8) | data[offset + 2];
}
uint16_t LATENT_CONTROL_BOX_CAN::parsePressureGage11(const uint8_t data[], size_t offset) {
    return (data[offset + 5] << 8) | data[offset + 4];
}
// 0x341210
// 故障位1解析
uint8_t LATENT_CONTROL_BOX_CAN::parseFaultBits1(const uint8_t* data, size_t offset) {
    return data[offset + 0];
}
// 故障位2解析
uint8_t LATENT_CONTROL_BOX_CAN::parseFaultBits2(const uint8_t* data, size_t offset) {
    return data[offset + 1];
}



// 状态检查实现
// 0x341101
int LATENT_CONTROL_BOX_CAN::checkWorkPattern(const uint8_t data[], size_t offset) {
    return parseWorkPattern(data, offset) > 1 ? 1 : 0;
}
int LATENT_CONTROL_BOX_CAN::checkSeaValve(const uint8_t data[], size_t offset) {
    return parseSeaValve(data, offset) > 2 ? 1 : 0;
}
int LATENT_CONTROL_BOX_CAN::checkVentValveB(const uint8_t data[], size_t offset) {
    return parseVentValveB(data, offset) > 2 ? 1 : 0;
}
int LATENT_CONTROL_BOX_CAN::checkWaterTankVentValveA1(const uint8_t data[], size_t offset) {
    return parseWaterTankVentValveA1(data, offset) > 2 ? 1 : 0;
}
int LATENT_CONTROL_BOX_CAN::checkWaterTankVentValveA2(const uint8_t data[], size_t offset) {
    return parseWaterTankVentValveA2(data, offset) > 2 ? 1 : 0;
}
int LATENT_CONTROL_BOX_CAN::checkWaterTankVentValveA3(const uint8_t data[], size_t offset) {
    return parseWaterTankVentValveA3(data, offset) > 2 ? 1 : 0;
}
int LATENT_CONTROL_BOX_CAN::checkWaterTankVentValveA4(const uint8_t data[], size_t offset) {
    return parseWaterTankVentValveA4(data, offset) > 2 ? 1 : 0;
}
// 0x341102
int LATENT_CONTROL_BOX_CAN::checkAirSourceSelection(const uint8_t data[], size_t offset) {
    return parseAirSourceSelection(data, offset) > 2 ? 1 : 0;
}

int LATENT_CONTROL_BOX_CAN::checkWaterTankBlow14(const uint8_t data[], size_t offset) {
    return parseWaterTankBlow14(data, offset) > 1 ? 1 : 0;
}

int LATENT_CONTROL_BOX_CAN::checkWaterTankBlow23(const uint8_t data[], size_t offset) {
    return parseWaterTankBlow23(data, offset) > 1 ? 1 : 0;
}
// 0x341203
int LATENT_CONTROL_BOX_CAN::checkOperationModeStatus(const uint8_t data[], size_t offset) {
    return parseOperationModeStatus(data, offset) > 1 ? 1 : 0;
}

int LATENT_CONTROL_BOX_CAN::checkWorkingModeStatus(const uint8_t data[], size_t offset) {
    return parseWorkingModeStatus(data, offset) > 1 ? 1 : 0;
}
// 0x341204
uint16_t LATENT_CONTROL_BOX_CAN::checkPort1TankLevel(const uint8_t data[], size_t offset) {
    uint16_t portlevel = parsePort1TankLevel(data, offset);
    return (portlevel > getTankLevelHeightThresholds().max_tanklevelheight) ? 1 : 0;
}

uint16_t LATENT_CONTROL_BOX_CAN::checkStarboard1TankLevel(const uint8_t data[], size_t offset) {
    uint16_t portlevel = parseStarboard1TankLevel(data, offset);
    return (portlevel > getTankLevelHeightThresholds().max_tanklevelheight) ? 1 : 0;
}

uint16_t LATENT_CONTROL_BOX_CAN::checkPort2TankLevel(const uint8_t data[], size_t offset) {
    uint16_t portlevel = parsePort2TankLevel(data, offset);
    return (portlevel > getTankLevelHeightThresholds().max_tanklevelheight) ? 1 : 0;
}

uint16_t LATENT_CONTROL_BOX_CAN::checkStarboard2TankLevel(const uint8_t data[], size_t offset) {
    uint16_t portlevel = parseStarboard2TankLevel(data, offset);
    return (portlevel > getTankLevelHeightThresholds().max_tanklevelheight) ? 1 : 0;
}
// 0x341205
uint16_t LATENT_CONTROL_BOX_CAN::checkPort3TankLevel(const uint8_t data[], size_t offset) {
    uint16_t portlevel = parsePort3TankLevel(data, offset);
    return (portlevel > getTankLevelHeightThresholds().max_tanklevelheight) ? 1 : 0;
}

uint16_t LATENT_CONTROL_BOX_CAN::checkStarboard3TankLevel(const uint8_t data[], size_t offset) {
    uint16_t portlevel = parseStarboard3TankLevel(data, offset);
    return (portlevel > getTankLevelHeightThresholds().max_tanklevelheight) ? 1 : 0;
}

uint16_t LATENT_CONTROL_BOX_CAN::checkPort4TankLevel(const uint8_t data[], size_t offset) {
    uint16_t portlevel = parsePort4TankLevel(data, offset);
    return (portlevel > getTankLevelHeightThresholds().max_tanklevelheight) ? 1 : 0;
}

uint16_t LATENT_CONTROL_BOX_CAN::checkStarboard4TankLevel(const uint8_t data[], size_t offset) {
    uint16_t portlevel = parseStarboard4TankLevel(data, offset);
    return (portlevel > getTankLevelHeightThresholds().max_tanklevelheight) ? 1 : 0;
}
// 0x341206
uint16_t LATENT_CONTROL_BOX_CAN::checkVentvalveAStatus(const uint8_t data[], size_t offset) {
    return parseVentvalveAStatus(data, offset) != 0 ? 1 : 0;
}
uint16_t LATENT_CONTROL_BOX_CAN::checkVentvalveBStatus(const uint8_t data[], size_t offset) {
    return parseVentvalveBStatus(data, offset) != 0 ? 1 : 0;
}
uint16_t LATENT_CONTROL_BOX_CAN::checkSeaValveStatus(const uint8_t data[], size_t offset) {
    return parseSeaValveStatus(data, offset) != 0 ? 1 : 0;
}
uint8_t LATENT_CONTROL_BOX_CAN::checkHighPressureBlowoutValveStatus(const uint8_t data[], size_t offset) {
    return parseHighPressureBlowoutValveStatus(data, offset) != 0 ? 1 : 0;
}
// 0x341207
uint16_t LATENT_CONTROL_BOX_CAN::checkPressureGage1(const uint8_t data[], size_t offset) {
    PressureGageThresholds thresholds = {60};
    setPressureGageThresholds(thresholds);
    uint16_t pressuregage = parsePressureGage1(data, offset);
    return (pressuregage > getPressureGageThresholds().max_pressuregage) ? 1 : 0;
}

uint16_t LATENT_CONTROL_BOX_CAN::checkPressureGage2(const uint8_t data[], size_t offset) {
    PressureGageThresholds thresholds = {60};
    setPressureGageThresholds(thresholds);
    uint16_t pressuregage = parsePressureGage1(data, offset);
    return (pressuregage > getPressureGageThresholds().max_pressuregage) ? 1 : 0;
}

uint16_t LATENT_CONTROL_BOX_CAN::checkPressureGage3(const uint8_t data[], size_t offset) {
    PressureGageThresholds thresholds = {60};
    setPressureGageThresholds(thresholds);
    uint16_t pressuregage = parsePressureGage1(data, offset);
    return (pressuregage > getPressureGageThresholds().max_pressuregage) ? 1 : 0;
}

uint16_t LATENT_CONTROL_BOX_CAN::checkPressureGage4(const uint8_t data[], size_t offset) {
    PressureGageThresholds thresholds = {60};
    setPressureGageThresholds(thresholds);
    uint16_t pressuregage = parsePressureGage1(data, offset);
    return (pressuregage > getPressureGageThresholds().max_pressuregage) ? 1 : 0;
}
// 0x341208
uint16_t LATENT_CONTROL_BOX_CAN::checkPressureGage5(const uint8_t data[], size_t offset) {
    PressureGageThresholds thresholds = {60};
    setPressureGageThresholds(thresholds);
    uint16_t pressuregage = parsePressureGage1(data, offset);
    return (pressuregage > getPressureGageThresholds().max_pressuregage) ? 1 : 0;
}

uint16_t LATENT_CONTROL_BOX_CAN::checkPressureGage6(const uint8_t data[], size_t offset) {
    PressureGageThresholds thresholds = {60};
    setPressureGageThresholds(thresholds);
    uint16_t pressuregage = parsePressureGage1(data, offset);
    return (pressuregage > getPressureGageThresholds().max_pressuregage) ? 1 : 0;
}


uint16_t LATENT_CONTROL_BOX_CAN::checkPressureGage7(const uint8_t data[], size_t offset) {
    PressureGageThresholds thresholds = {60};
    setPressureGageThresholds(thresholds);
    uint16_t pressuregage = parsePressureGage1(data, offset);
    return (pressuregage > getPressureGageThresholds().max_pressuregage) ? 1 : 0;
}

uint16_t LATENT_CONTROL_BOX_CAN::checkPressureGage8(const uint8_t data[], size_t offset) {
    PressureGageThresholds thresholds = {60};
    setPressureGageThresholds(thresholds);
    uint16_t pressuregage = parsePressureGage1(data, offset);
    return (pressuregage > getPressureGageThresholds().max_pressuregage) ? 1 : 0;
}
// 0x341209
uint16_t LATENT_CONTROL_BOX_CAN::checkPressureGage9(const uint8_t data[], size_t offset) {
    PressureGageThresholds thresholds = {60};
    setPressureGageThresholds(thresholds);
    uint16_t pressuregage = parsePressureGage1(data, offset);
    return (pressuregage > getPressureGageThresholds().max_pressuregage) ? 1 : 0;
}
uint16_t LATENT_CONTROL_BOX_CAN::checkPressureGage10(const uint8_t data[], size_t offset) {
    PressureGageThresholds thresholds = {5};
    setPressureGageThresholds(thresholds);
    uint16_t pressuregage = parsePressureGage1(data, offset);
    return (pressuregage > getPressureGageThresholds().max_pressuregage) ? 1 : 0;
}

uint16_t LATENT_CONTROL_BOX_CAN::checkPressureGage11(const uint8_t data[], size_t offset) {
    PressureGageThresholds thresholds = {5};
    setPressureGageThresholds(thresholds);
    uint16_t pressuregage = parsePressureGage1(data, offset);
    return (pressuregage > getPressureGageThresholds().max_pressuregage) ? 1 : 0;
}
// 0x341210
int LATENT_CONTROL_BOX_CAN::checkFaultBits1Status(const uint8_t* data, size_t offset) {
    return parseFaultBits1(data, offset) != 0 ? 1 : 0;
}
int LATENT_CONTROL_BOX_CAN::checkFaultBits2Status(const uint8_t* data, size_t offset) {
    return parseFaultBits2(data, offset) != 0 ? 1 : 0;
}